package com.weidong.zk.controller;

import lombok.extern.java.Log;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 * @Author weidong
 * @Date 2020/3/19 20:24
 * @Version 1.0
 * @Description 用户登录
 **/
@Log
public class LoginController extends GenericForwardComposer {


    private Textbox userName, userPwd;

    String userId = "";
    String passWord = "";
    String loginIP = "";

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        log.info("LoginController is doAfterCompose() start");
        SpringUtil.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);
        super.doAfterCompose(comp);
    }

    public void onClick$toLogin() {
        System.out.println("====Login_Ctrl====Login===Start==");
        log.info("====LoginController====onLogin===Start==");

        int resultCode = -1;

        if (userName.getValue() != null && !userName.getValue().equals("")) {
            userId = userName.getValue().trim();
        }

        if (userPwd.getValue() != null && !userPwd.getValue().equals("")) {
            passWord = userPwd.getValue().trim();
        }

        System.out.println("UserId======" + userId);
        System.out.println("Password====" + passWord);
        log.info("====UserId=={},Password===={}"+userId+passWord);

        try {

            // 取登入者IP
            if (Sessions.getCurrent().getRemoteAddr() != null && !Sessions.getCurrent().getRemoteAddr().equals("")) {
                loginIP = Sessions.getCurrent().getRemoteAddr().trim();
            }

            // 有任一账号或密码为空则停止
            if (userId == null   || "".equals(userId.trim())   || userId.isEmpty() ||
                    passWord == null || "".equals(passWord.trim()) ||userId.isEmpty() ) {
                Messagebox.show("输入之账号或密码不对, 请检查后再输入!!! ", "警告讯息!!! ", Messagebox.OK, Messagebox.ERROR);
                return;
            }

            // 身分验证成功，将用户信息放至 Session 中
//			V_USR_ROLE user2 = new V_USR_ROLE();
//			resultCode = loginService.Login(userId, passWord, loginIP);

            // 设定登入者数据, 放入 Session
//			V_USR_ROLE loginuser = new LoginUser(user2, loginIP);
//			Sessions.getCurrent(true);
//			Sessions.getCurrent().setAttribute("AMS.LoginUser", loginuser);

//			System.out.println("==========go to MENU============="+RedisKeyConstant.getRedis2KValue("AMS.2k.DefaultPage.MENU#"));
//			Executions.getCurrent().sendRedirect(RedisKeyConstant.getRedis2KValue(RedisKeyConstant.MENU_PAGE));
            Executions.getCurrent().sendRedirect("~./zul/HomePage.zul");
            return;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
        }
    }
}
